# -*- coding: utf-8 -*-
# Project : flacon
# File : flac_encoder.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

from PyQt4.QtCore import *
import flaconglobal

from encoderConfigPage import EncoderConfigPage
from mp3_config_ui import Ui_mp3ConfigPage as UI


class Mp3Config(EncoderConfigPage, UI):

   #****************************************************************
   #
   #****************************************************************
    @staticmethod
    def defaults():
        return {
            'Mp3/Preset':           'vbrStandardFast',
            'Mp3/Bitrate':          '320',
            'Mp3/Quality':          4,
            'Mp3/ReplayGain':       flaconglobal.ReplayGainTypes.Disable,
        }


   #****************************************************************
   #
   #****************************************************************
    def __init__(self, parent):
        EncoderConfigPage.__init__(self, parent)
        self.setupUi(self)


        presets = self.mp3PresetCbx

        presets.addItem(self.tr('VBR medium'),        QVariant('vbrMedium'))
        presets.addItem(self.tr('VBR standard'),      QVariant('vbrStandard'))
        presets.addItem(self.tr('VBR standard fast'), QVariant('vbrStandardFast'))
        presets.addItem(self.tr('VBR extreme'),       QVariant('vbrExtreme'))
        presets.addItem(self.tr('VBR extreme fast'),  QVariant('vbrExtremeFast'))
        presets.addItem(self.tr('VBR quality'),       QVariant('vbrQuality'))
        presets.addItem(self.tr('CBR insane'),        QVariant('cbrInsane'))
        presets.addItem(self.tr('CBR kbps'),          QVariant('cbrKbps'))
        presets.addItem(self.tr('ABR kbps'),          QVariant('abrKbps'))


        bitrates = self.mp3BitrateCbx
        self.fillBitrateComboBox(self.mp3BitrateCbx, [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320])

        css = '''<style type="text/css">
              qbody { font-size: 9px; }
              dt { font-weight: bold; }
              dd { margin-left: 8px; margin-bottom: 8px; }
              </style>'''
        self.mp3PresetCbx.setToolTip(css + self.mp3PresetCbx.toolTip());

        self.setLossyToolTip(self.mp3QualitySlider)
        self.mp3QualitySpin.setToolTip(self.mp3QualitySlider.toolTip())
        self.mp3QualityLabel.setToolTip(self.mp3QualitySlider.toolTip())

        self.connect(self.mp3PresetCbx, SIGNAL('currentIndexChanged(int)'), self.mp3PresetCbxCanged)
        self.mp3PresetCbxCanged(self.mp3PresetCbx.currentIndex())

        self.fillReplayGainComboBox(self.mp3GainCbx)

        self.loadComboBox('Mp3/Preset',      self.mp3PresetCbx)
        self.loadComboBox('Mp3/Bitrate',     self.mp3BitrateCbx)
        self.loadSpinBox('Mp3/Quality',      self.mp3QualitySpin)
        self.mp3QualitySlider.setValue(self.mp3QualitySpin.value())
        self.loadComboBox('Mp3/ReplayGain',  self.mp3GainCbx)


   #****************************************************************
   #
   #****************************************************************
    def write(self):
        self.writeComboBox('Mp3/Preset',    self.mp3PresetCbx)
        self.writeComboBox('Mp3/Bitrate',   self.mp3BitrateCbx)
        self.writeSpinBox('Mp3/Quality',    self.mp3QualitySpin)
        self.writeComboBox('Mp3/ReplayGain',self.mp3GainCbx)


   #****************************************************************
   #
   #****************************************************************
    def mp3PresetCbxCanged(self, index):
        preset = self.mp3PresetCbx.itemData(index).toString()

        enable = (preset == 'abrKbps' or preset=='cbrKbps')
        self.mp3BitrateBox.setEnabled(enable)

        enable = (preset == 'vbrQuality')
        self.mp3QualityBox.setEnabled(enable)

